package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.OrderSettinglMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettinglMapper, OrderSetting> implements OrderSettingService {


    @Override
    public void upOrderSettings(List<String[]> orderSettingList) {
        //添加 saveBatch 加上缓存桶 性能更好
        //表中有主键 用户上传一次后 可能还上传一次一样的 ,但是数据库不能存同一日期的信息
        //所以如果第二次提交后,相同的信息 需要改成修改的方法 执行
        // 根据日期 判断 如果数据库 已经存在 日期 进行update 最大预约
        //如果数据库  没有指定日期记录 进行 批量insert
        //写个方法把 orderSettingList 里面的数据转化为 List<OrderSetting>
        List<OrderSetting> orderSettingImportList = changeStringArrayToList(orderSettingList);
        //这个方法 把数组 转化为集合对象

        //判断数据库是不是已经存在 对应的日期数据
        //如果存在  集合里面要有主键 放在orderSettingImportList里面 执行update

        //进行业务处理 不然一直都是添加
        for (OrderSetting orderSetting : orderSettingImportList) {
            //判断日期存在不 如果存在 进行主键添加
            //todo:新加
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
            queryWrapper.eq("orderdate", orderSetting.getOrderDate()); //  where  orderdate = '2020-11-9'
            OrderSetting orderSettingIsOrNotExist = baseMapper.selectOne(queryWrapper);
            if (orderSettingIsOrNotExist != null) {
                //  update
                orderSetting.setId(orderSettingIsOrNotExist.getId());
            }
        }

        //第一次的 直接执行 添加  mp是根据主键 判断 save 还是update
        saveOrUpdateBatch(orderSettingImportList);
    }

    //todo: 根据年月 查询出具体日期的 最大预约数 和 已经预约数
    @Override
    public Map findSettingData(int year, int month) {
        String beginDate = year + "-" + month + "-1"; //2020-8-1
        String endDarte = year + "-" + month + "-31";//2020-8-31 不管一个跳跃多少天都是31天

       /* QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
        queryWrapper.between("orderdate", beginDate, endDarte);
        List<OrderSetting> orderSettings = baseMapper.selectList(queryWrapper);这样查询的不用谢mapper层 但是会查询表中所有的值
*/

        List<OrderSetting> orderSettings = baseMapper.findSettingDateByYearAndMonth(beginDate,endDarte);


        //Map
        Map<String, Object> map = new HashMap();
        for (OrderSetting orderSetting : orderSettings) {

            Date orderDate = orderSetting.getOrderDate();
            //把日期对象转化为字符串
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            //把前端需要的信息放进去 "2020-11-01":{number:200,reservations:200},
            //前面日期 后面orderSetting的对象中的两个属性

            /*OrderSetting orderSettingVO = new OrderSetting();
            orderSettingVO.setNumber(orderSetting.getNumber());
            orderSettingVO.setReservations(orderSetting.getReservations());
            */
            //也可以在创建一个map
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());

            map.put(dateString, orderSettingMap);
        }
        return map;
    }

    private List<OrderSetting> changeStringArrayToList(List<String[]> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() != 0) {
            List<OrderSetting> orderSettings = new ArrayList<OrderSetting>();
            for (String[] strings : orderSettingList) {


                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setReservations(0);//已经预约数 用户发起的请求
                orderSetting.setNumber(Integer.parseInt(strings[1]));  //可以预约数
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                //用日期工具类把string转化为日期


                orderSettings.add(orderSetting);
            }
            return orderSettings;
        }

        return null;
    }
}
